#include "head.h"

sqlite3 *db = NULL;  //数据库句柄

MSG msg;
/**
 ** @file:	   head.h
 ** @author:   王建
 ** @date:	   2017.3.21
 ** @brief:    登录
 */
void do_login(int sockfd, MSG *pbuf)
{
	//printf("%s",);
	if(in_staff(sockfd,pbuf) == 1)
	{	printf("是普通员工\n");	
		msg.ret = STAFF;
		send(sockfd,pbuf,sizeof(MSG),0);
		
	}if(in_admain(sockfd,pbuf) == 1)
	{
		printf("是管理员\n");	
		msg.ret = ADMIN;
		send(sockfd,pbuf,sizeof(MSG),0);
		
	}else{
		msg.ret = FAILED;
		send(sockfd,pbuf,sizeof(MSG),0);
		printf("没有\n");	
	}
}

/**
 ** @file:	   head.h
 ** @author:   王建
 ** @date:	   2017.3.21
 ** @brief:    员工判断
 */
int in_staff(int sockfd,MSG *pbuf)
{	int nrow,ncolumn;
	char sql[256] = {0} ;
	char **resultp;
	char *errmsg;
	sprintf(sql,"select * from company where name = '%s' and id = '%s'",pbuf->name,pbuf->passwd);
	if (sqlite3_get_table(db, sql, &resultp, &nrow, &ncolumn, &errmsg) != SQLITE_OK)
	{
		printf("%s\n",errmsg);
		strcpy(msg.data,"员工判断错误");
		send(sockfd,pbuf,sizeof(MSG),0);
	}
	else{
			if(nrow ==0)
			{
				printf("没有yi条数\n");
				return 0;
			}else{
				printf("找到\n");
					return 1; 
				}
		}
	
}

/**
 ** @file:	   head.h
 ** @author:   王建
 ** @date:	   2017.3.21
 ** @brief:    管理员判断
 */
int in_admain(int sockfd,MSG *pbuf)
{
	int nrow,ncolumn;
	char sql[256] = {0} ;
	char **resultp;
	char *errmsg;
	sprintf(sql,"select * from company where name = '%s' and id = '%s'",pbuf->name,pbuf->passwd);
	if (sqlite3_get_table(db, sql, &resultp, &nrow, &ncolumn, &errmsg) != SQLITE_OK)
	{
		printf("%s\n",errmsg);
		strcpy(msg.data,"管理员判断错误");
		send(sockfd,pbuf,sizeof(MSG),0);
	}
	else{
			if(nrow ==0)
			{
				return 0;
				
			}else{
		
					return 1; 
				}
		}
	
}

/**
 ** @file:	   head.h
 ** @author:   王建
 ** @date:	   2017.3.21
 ** @brief:    员工自己改密码
 */

void do_staff_change_passwd(int sockfd, MSG *pbuf)
{
	char sqlcmd[256] = {0};
	char *errmsg;
	sprintf(sqlcmd,"select * from company where name = '%s' and id = '%s'",pbuf->name,pbuf->passwd);
	if(sqlite3_exec(db,sqlcmd, NULL,NULL,&errmsg) != SQLITE_OK)
	{
		//printf("%s\n"errmsg);
		printf("do_staff_change_passwd错误\n");
		pbuf->ret = FAILED;
		send(sockfd,pbuf,sizeof(MSG),0);
		return;
	}else{
		
		pbuf->ret = SUCCESS;
		//usleep(1000);
		send(sockfd,pbuf,sizeof(MSG),0);
		return;
	}	                             
}
//管理员修改用户密码
void do_admin_add_change_passwd(int sockfd, MSG *pbuf)
{
	
	
}
//做员工查询
void do_staff_query(int sockfd, MSG *pbuf)
{
	
    
}
//做管理员查询
void do_admin_query(int sockfd, MSG *pbuf)
{
    
}

void do_admin_del(int sockfd,MSG *pbuf)
{
	
	
}
void do_admin_add(int sockfd,MSG *pbuf)
{
	
	
	
}

void *handler(void * arg)
{
     MSG msg;
    int n;
    int connfd = (int)arg;

    while(1)
    {
        n = recv(connfd, &msg, sizeof(MSG), 0);
        //printf("name = %s\n", msg.name);
        if(n == 0){  //客户端退出
            pthread_exit(NULL);
            close(connfd);
        }else if(n > 0){  //收到信息
            //printf("recv type = %d\n", msg.type);
            switch (msg.type) {
            case LOGIN:  //登录
                do_login(connfd, &msg);
                break;
            case STAFF_CHANGE_PASSWD:
                do_staff_change_passwd(connfd, &msg);
                break;
            case STAFF_QUERY:
                do_staff_query(connfd, &msg);
                break;
            case ADMIN_QUERY:
                do_admin_query(connfd, &msg);
                break;
            case ADMIN_DEL_STAFF:
                do_admin_del(connfd, &msg);
                break;
            case ADMIN_ADD_STAFF:
                do_admin_add(connfd, &msg);
                break;
            default:
                break;
            }
        }else{
            printf("abc");
            break;
        }
    }
    close(connfd);
    pthread_exit(NULL);
}

int main(int argc, char *argv[])
{
    int listenfd, connfd;
    struct sockaddr_in myaddr;

    if (argc < 3)
    {
        printf("Usage : %s <ip> <port>\n", argv[0]);
        exit(-1);
    }

    //打开数据库
    if(sqlite3_open(DATABASE, &db) < 0){
        printf("fail to sqlite3_open : %s\n", sqlite3_errmsg(db));
        return -1;
    }

    //创建服务器socket
    listenfd = socket(PF_INET, SOCK_STREAM, 0);
    if(listenfd < 0){
        perror("fail to socket");
        exit(-1);
    }
    bzero(&myaddr, sizeof(myaddr));
    myaddr.sin_family = PF_INET;
    myaddr.sin_port = htons(atoi(argv[2]));
    myaddr.sin_addr.s_addr = inet_addr(argv[1]);
    if (bind(listenfd, (SA *)&myaddr, sizeof(myaddr)) < 0)
    {
        perror("fail to bind");
        exit(-1);
    }

    // XXX int listen(int sockfd, int backlog);
    if (listen(listenfd, 5) < 0)
    {
        perror("fail to listen");
        exit(-1);
    }

    //接受客户端请求，创建子线程
    while(1){
        if((connfd = accept(listenfd, NULL, NULL)) < 0){
            perror("fail to accept");
            exit(-1);
        }
        pthread_t child_pthread;
        if(pthread_create(&child_pthread, NULL, handler,(void *)connfd) < 0)
        {
            perror("pthread_create");
            exit(1);
        }		
    }
}
